package org.idea.spring.task.service;

import org.idea.spring.task.config.SqlSessionUtil;
import org.idea.spring.task.dao.CronSettingDao;
import org.idea.spring.task.model.CronSetting;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

/**
 * @author idea
 * @data 2019/1/15
 */
@Service
public class CronSettingService {

    public CronSettingDao cronSettingDao;

    private static ConcurrentHashMap<String, ScheduledFuture<?>> cmap = new ConcurrentHashMap<>();

    public static boolean putTaskBean(String name, ScheduledFuture<?> obj) {
        cmap.put(name, obj);
        return true;
    }

    public static ScheduledFuture<?> getBean(String name) {
        return cmap.get(name);
    }

    /**
     * 获取定时任务map
     *
     * @return
     */
    public static ConcurrentHashMap getTaskMap() {
        return cmap;
    }


    public List<CronSetting> selectAllCron() {
        cronSettingDao = (CronSettingDao) SqlSessionUtil.getSqlSession().getMapper(CronSettingDao.class);
        return cronSettingDao.selectAllCron();
    }

    public CronSetting selectOneByName(String name) {
        cronSettingDao = (CronSettingDao) SqlSessionUtil.getSqlSession().getMapper(CronSettingDao.class);
        return cronSettingDao.selectOneByCronName(name);
    }

}
